<template>
  <div class="app-container">
    <el-dialog :title="titles[flag]" :visible.sync="dialogVisible" width="50%" append-to-body>
      <div class="myTitle">基本信息</div>
      <el-form ref="form" :model="form" :disabled="flag==1">
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="订单编号" prop="orderNo" required>
              <div v-if="flag==1">{{form.orderNo}}</div>
              <el-input v-else v-model="form.orderNo" placeholder="自动生成" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收款日期" prop="orderDate" required>
              <div v-if="flag==1">{{form.orderDate}}</div>
              <el-input v-else v-model="form.orderDate" placeholder="自动生成" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="往来单位" prop="customerId">
              <div v-if="flag==1">{{form.merchantName}}</div>
              <el-select v-else v-model="form.customerId" filterable style="width: 100%">
                <el-option
                  v-for="item in unitList"
                  :key="item.id"
                  :label="item.merchantName"
                  :value="item.id.toString()">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="支付方式" prop="paymentId" required>
              <div v-if="flag==1">{{form.paymentName}}</div>
              <el-select v-else v-model="form.paymentId" filterable style="width: 100%">
                <el-option
                  v-for="item in payList"
                  :key="item.id"
                  :label="item.paymentName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单据类型" prop="receiptType" required>
              <dict-tag v-if="flag==1" :options="dict.type.ims_receipt_type" :value="form.receiptType"/>
              <el-select v-else v-model="form.receiptType" filterable style="width: 100%">
                <el-option
                  v-for="item in dict.type.ims_receipt_type"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合计金额(元)" prop="incomes" required>
              <div v-if="flag==1">{{form.incomes}}</div>
              <el-input v-else type="number" v-model="form.incomes" placeholder="请输入" :min="0"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="付款凭证" prop="imageUrl" required>
              <div v-if="flag==1">
                <div class="myImg" v-if="form.imageUrl">
                  <el-image
                    :src="form.imageUrl"
                    fit="contain"
                    :preview-src-list="[form.imageUrl]"
                  >
                  </el-image>
                </div>
                <div v-else>暂无</div>
              </div>
              <div v-else>
                <div class="myImg" v-if="form.imageUrl">
                  <i class="el-icon-close" @click="form.imageUrl=null"></i>
                  <el-image
                    :src="form.imageUrl"
                    fit="contain"
                    :preview-src-list="[form.imageUrl]"
                  >
                  </el-image>
                </div>
                <el-upload
                  v-else
                  :headers="headers"
                  :action="action"
                  :on-success="uploadSuccess"
                  :on-progress="uploadProgress"
                  :show-file-list="false"
                  accept=".jpg,.jpeg,.png"
                >
                  <el-button type="primary" plain>点击上传</el-button>
                </el-upload>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="备注" prop="remark">
              <div v-if="flag==1">{{form.remark}}</div>
              <el-input v-else type="textarea" v-model="form.remark" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div v-if="flag==1">
        <div class="myTitle">收入信息</div>
        <el-table
          :data="itemList"
          max-height="500">
          <el-table-column type="index" label="序号" align="center"></el-table-column>
          <el-table-column prop="relationOrderNo" label="关联单据" align="center"/>
          <el-table-column prop="orderNo" label="业务类型" align="center">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.ims_receipt_docs_type" :value="scope.row.relationOrderType"/>
            </template>
          </el-table-column>
          <el-table-column prop="orderNo" label="核销金额(元)" align="center">
            <template slot-scope="scope">
              <div>{{ scope.row.incomes }}</div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addPayOrder,
  getPayOrder,
  getPayOrderCode,
  listCustomOrder,
  listPayType,
  updatePayOrder,
} from "../../../../api/payMng";
import {listUnit} from "../../../../api/goodMng";

export default {
  dicts: ['ims_receipt_type','ims_receipt_docs_type'],
  data() {
    return {
      flag: 0,
      titles: ['新增', '详情', '修改'],
      loading: false,
      dialogVisible: false,
      form: {},
      unitList: [],
      payList: [],
      projectList: [],
      itemList: [],
      action: process.env.VUE_APP_BASE_API + '/ims/product/image/upload',
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.form = {}
      this.resetForm('form')
      this.itemList = []
      if(this.flag!=1){
        this.load()
      }
      if (this.flag == 0) {
        getPayOrderCode().then(res => {
          this.$set(this.form, 'orderNo', res.data.orderNo)
          this.$set(this.form, 'orderDate', res.data.orderDate)
        })
      } else {
        getPayOrder(form.id).then(res => {
          this.form = res.data
          this.itemList = this.form.items
        })
      }
    },
    load() {
      listUnit({pageSize: -1}).then(res => {
        this.unitList = res.rows
      })
      listPayType({pageSize: -1}).then(res => {
        this.payList = res.rows
      })
      listCustomOrder({customerId: 3, status: 80}).then(res => {
        this.projectList = res.data
      })
    },
    uploadProgress() {
      this.loading = true
    },
    uploadSuccess(res) {
      this.loading = false
      if (res.code == 200) {
        this.$set(this.form, 'imageUrl', res.data.imageUrl)
      } else {
        this.$message.error(res.msg)
      }
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.loading = true
          if (this.flag == 0) {
            addPayOrder(this.form).then(res => {
              this.dialogVisible = false
              this.$message.success('新增成功')
              this.$emit('ok')
            }).finally(e => {
              this.loading = false
            })
          } else {
            updatePayOrder(this.form).then(res => {
              this.dialogVisible = false
              this.$message.success('修改成功')
              this.$emit('ok')
            }).finally(e => {
              this.loading = false
            })
          }
        }
      })
    },
  }
}
</script>
